package com.djp.smartapp.adapter;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.djp.smartapp.MainActivity;
import com.djp.smartapp.bean.UserPeopleResponse;
import com.djp.smartapp.bean.NoDataResponse;
import com.djp.smartapp.dialog.GetPeopleDialog;
import com.djp.smartapp.dialog.UniversalDialog;
import com.djp.smartapp.util.Line;
import com.djp.smartapp.util.OkHttpUtil;
import com.djp.smartapp.util.LineSpUtil;
import com.google.gson.Gson;
import com.lenovo.manufacture.R;

import java.util.List;


public class PeopleListAdapter extends BaseAdapter {
    private List<UserPeopleResponse.UserPeople> userPeopleList;
    private MainActivity context;
    private int userPLId;
    private int PLId;
    private Gson gson;
    private GetPeopleDialog dialog;
    private static final String TAG = "PeopleListAdapter";


    public PeopleListAdapter(List<UserPeopleResponse.UserPeople> userPeopleList, MainActivity context, GetPeopleDialog dialog) {
        this.userPeopleList = userPeopleList;
        this.context = context;
        this.userPLId = LineSpUtil.getUserLineId(context);
        this.dialog = dialog;
        this.PLId = LineSpUtil.getLineId(context);
    }

    public void setUserPeopleList(List<UserPeopleResponse.UserPeople> userPeopleList) {
        this.userPeopleList = userPeopleList;
    }

    @Override
    public int getCount() {
        return userPeopleList.size();
    }

    @Override
    public Object getItem(int i) {
        return userPeopleList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        if (view == null) {
            view = LayoutInflater.from(context).inflate(R.layout.item_dialog_people_list, viewGroup, false);
            view.setTag(new ViewHolder(view));
        }
        ViewHolder holder = (ViewHolder) view.getTag();
        //Log.d(TAG, "getView: 目前item信息"+userPeopleList.get(i).toString());
        holder.tvPeopleId.setText(userPeopleList.get(i).getId() + "");
        holder.tvPeopleHp.setText(userPeopleList.get(i).getPower() + "");
        holder.tvPeopleMoney.setText(userPeopleList.get(i).getMoney() + "");
        holder.tvPeopleName.setText(userPeopleList.get(i).getName());
        holder.tvPeopleType.setText(type(userPeopleList.get(i).getWorkType()));
        try {
            holder.tvPeopleWork.setText(userPeopleList.get(i).getWorkPost().getWorkPostName());
        } catch (NullPointerException e) {
            holder.tvPeopleWork.setText("无岗位");
        }

        //判读该员工是否已有工作，若有工作则不可分配
        if (userPeopleList.get(i).getUserProductionLineId() != -1) {
            holder.btnGetPeople.setEnabled(false);
            holder.btnGetPeople.setText("已在工作");
        } else {
            holder.btnGetPeople.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    sendGetPeopleReq(userPeopleList.get(i));
                }
            });
        }
        return view;
    }


    private int selectPeopleWork(UserPeopleResponse.UserPeople userPeople) {
        return (userPeople.getWorkType()+1)+(4*(PLId-1));
    }

    private String type(int type) {
        switch (type) {
            case 0:
                return "工程师";
            case 1:
                return "工人";
            case 2:
                return "技术人员";
            case 3:
                return "检测人员";
            default:
                return "错误岗位";
        }
    }


    private void sendGetPeopleReq(final UserPeopleResponse.UserPeople userPeople) {
        new Thread() {
            @Override
            public void run() {
                if (Line.lineIsReady(context)) {
                    OkHttpUtil okHttpUtil = OkHttpUtil.getOkHttpUtil();
                    String reqBody = "userPeopleId=" + userPeople.getId() + "&userLineId=" + userPLId + "&positionId="+selectPeopleWork(userPeople);
                    Log.d(TAG, "run: "+reqBody);
                    String postRes = okHttpUtil.getPostRes("index/assignUserPosition", reqBody);
                    gson = new Gson();
                    NoDataResponse res = gson.fromJson(postRes, NoDataResponse.class);

                    if (res.getStatus().equals("200")) {
                        context.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                Toast.makeText(context, "分配成功", Toast.LENGTH_SHORT).show();
                            }
                        });
                        dialog.requestToRefreshList();
                    }
                } else {
                    context.runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            dialog.dismiss();
                            Log.d(TAG, "run: 你还没有创建生产线");
                            Toast.makeText(context, "你还没有创建生产线", Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            }
        }.start();
    }


    class ViewHolder {
        TextView tvPeopleId;
        TextView tvPeopleName;
        TextView tvPeopleType;
        TextView tvPeopleMoney;
        TextView tvPeopleHp;
        TextView tvPeopleWork;
        Button btnGetPeople;

        public ViewHolder(View view) {
            tvPeopleId = view.findViewById(R.id.item_tv_people_id);
            tvPeopleName = view.findViewById(R.id.item_tv_people_name);
            tvPeopleWork = view.findViewById(R.id.item_tv_people_type);
            tvPeopleMoney = view.findViewById(R.id.item_tv_people_money);
            tvPeopleType = view.findViewById(R.id.item_tv_people_can_work);
            tvPeopleHp = view.findViewById(R.id.item_tv_people_hp);
            btnGetPeople = view.findViewById(R.id.item_btn_people_get);
        }
    }
}

